<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_Form_Create extends Engine_Form {
	public $_error = array ();
	
	public function init() {
		$this->setTitle ( 'Post New Page' )->setDescription ( 'Please fill out the form below, then click "Post Page" to submit your page.' )->setAttrib ( 'name', 'cmspages_create' );
		
		$user = Engine_Api::_ ()->user ()->getViewer ();
		$user_level = Engine_Api::_ ()->user ()->getViewer ()->level_id;
		
		// prepare categories
		$categories = Engine_Api::_ ()->cmspage ()->getCategories ();
		if (count ( $categories ) != 0) {
			$categories_prepared = Engine_Api::_ ()->cmspage ()->convertCategoriesToArray ( $categories );
			$categories_prepared = array ("" => "" ) + $categories_prepared;
			
			// category field
			$this->addElement ( 'Select', 'category_id', array ('label' => 'Category', 'multiOptions' => $categories_prepared, 'allowEmpty' => false, 'required' => true, 'validators' => array (array ('NotEmpty', true ) ), 'filters' => array ('Int' ) ) );
		}
		
		$this->addElement ( 'Text', 'title', array ('label' => 'Title', //  'description' => 'Enter name of this location. Example: Sweet Home',
		'allowEmpty' => false, 'required' => true, 'validators' => array (array ('NotEmpty', true ), array ('StringLength', false, array (1, 128 ) ) ), 'filters' => array ('StripTags', new Engine_Filter_Censor () ) ) );
		
		$allowed_html = Engine_Api::_ ()->authorization ()->getPermission ( $user_level, 'cmspage', 'auth_html' );
		$allowed_htmlattrs = str_replace ( " ", "", Engine_Api::_ ()->authorization ()->getPermission ( $user_level, 'cmspage', 'auth_htmlattrs' ) );
		
		$this->addElement ( 'TinyMce', 'body', array ('label' => 'Body Content', 'required' => true, 'allowEmpty' => false, 'filters' => array (new Engine_Filter_Censor (), new Engine_Filter_Html ( array ('allowedTags' => $allowed_html, 'useDefaultLists' => false, 'allowedAttributes' => $allowed_htmlattrs ) ) ), 'editorOptions' => array ('mode' => 'exact', 'elements' => 'body', 'width' => 580, 'height' => 400, 'plugins' => "emotions, table, fullscreen, media, preview, paste, style, layer, xhtmlxtras", 'theme_advanced_buttons1' => "cut,copy,paste,pastetext,pasteword,|,undo,redo,|,link,unlink,anchor,charmap,image,media,|,justifyleft,justifycenter,justifyright,justifyfull,|,hr,removeformat,cleanup,code,|,preview", 'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,|,bullist,numlist,|,outdent,indent,blockquote,|,sub,sup,|,tablecontrols", 'theme_advanced_buttons3' => "formatselect,fontselect,fontsizeselect,|,forecolor,backcolor,|,insertlayer,moveforward,movebackward,absolute,|,styleprops,attribs,|,cite,del,ins" ) ) );
		
		$this->addElement ( 'Textarea', 'description', array ('label' => 'Page Description', // 'description' => 'Maximum: 500 characters',
		'validators' => array (array ('NotEmpty', true ), array ('StringLength', false, array (1, 500 ) ) ), 'filters' => array ('StripTags', new Engine_Filter_Censor (), new Engine_Filter_HtmlSpecialChars () ) ) );
		$this->description->getDecorator ( "Description" )->setOption ( "placement", "append" );
		
		/*
    $allowed_upload = Engine_Api::_()->authorization()->getPermission($user_level, 'cmspage', 'photo');
    if($allowed_upload){
      $this->addElement('File', 'photo', array(
        'label' => 'Main Photo'
      ));
      $this->photo->addValidator('Extension', false, 'jpg,png,gif');
    }
    */
		
		$this->addElement ( 'Text', 'tags', array ('label' => 'Tags (Keywords)', 'autocomplete' => 'off', 'description' => 'Separate tags with commas.', 'filters' => array ('StripTags', new Engine_Filter_Censor () ) ) );
		$this->tags->getDecorator ( "Description" )->setOption ( "placement", "append" );
		
		$allowed_script = Engine_Api::_ ()->authorization ()->getPermission ( $user_level, 'cmspage', 'script' );
		if ($allowed_script) {
			$this->addElement ( 'Textarea', 'header_script', array ('label' => 'Custom JavaScript Code' ) );
		}
		$allowed_style = Engine_Api::_ ()->authorization ()->getPermission ( $user_level, 'cmspage', 'style' );
		if ($allowed_style) {
			$this->addElement ( 'Textarea', 'header_style', array ('label' => 'Custom Style Code (CSS)' ) );
		}
		
		// Add subforms
		if (! $this->_item) {
			$customFields = new Cmspage_Form_Custom_Fields ();
		} else
			$customFields = new Cmspage_Form_Custom_Fields ( array ('item' => $this->getItem () ) );
		
		$this->addSubForms ( array ('customField' => $customFields ) );
		
		// View
		$availableLabels = array ('everyone' => 'Everyone', 'registered' => 'Registered Members', 'owner_network' => 'Friends and Networks', 'owner_member_member' => 'Friends of Friends', 'owner_member' => 'Friends Only', 'owner' => 'Just Me' );
		
		$options = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'cmspage', $user, 'auth_view' );
		$options = array_intersect_key ( $availableLabels, array_flip ( $options ) );
		
		$this->addElement ( 'Select', 'auth_view', array ('label' => 'Privacy', 'description' => 'Who may see this page?', 'multiOptions' => $options, 'value' => 'everyone' ) );
		$this->auth_view->getDecorator ( 'Description' )->setOption ( 'placement', 'prepend' );
		
		$options = ( array ) Engine_Api::_ ()->authorization ()->getAdapter ( 'levels' )->getAllowed ( 'cmspage', $user, 'auth_comment' );
		$options = array_intersect_key ( $availableLabels, array_flip ( $options ) );
		
		// Comment
		$this->addElement ( 'Select', 'auth_comment', array ('label' => 'Comment Privacy', 'description' => 'Who may post comments on this page?', 'multiOptions' => $options, 'value' => 'registered' ) );
		$this->auth_comment->getDecorator ( 'Description' )->setOption ( 'placement', 'prepend' );
		
		if (! $this->_item || ($this->_item && ! $this->_item->published)) {
			$this->addElement ( 'Radio', 'published', array ('label' => 'Status', 'allowEmpty' => false, 'required' => true, 'multiOptions' => array ("1" => "Published", "0" => "Saved As Draft" ), 'description' => "If page is published, it cannot be switched back to draft mode." ) );
			$this->published->getDecorator ( 'Description' )->setOption ( 'placement', 'prepend' );
		} else if ($this->_item && $this->_item->published) {
			$this->addElement ( 'Radio', 'maintainance', array ('label' => 'Maintenance Mode', 'allowEmpty' => false, 'required' => true, 'multiOptions' => array ("0" => "Online", "1" => "Offline (Maintenance Mode)" ), 'value' => "0", 'description' => "Would you like to temporarily prevent site visitors from accessing this page?" ) );
			$this->maintainance->getDecorator ( 'Description' )->setOption ( 'placement', 'prepend' );
		}
		
		$check_fields = array ('showlayout' => 1, 'showtitle' => 0, 'showphoto' => 0, 'showinfo' => 0, 'showtool' => 0, 'showcomment' => 0 );
		foreach ( $check_fields as $field_key => $field_default ) {
			$setting_field_value = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'cmspage.' . $field_key, 2 );
			if ($setting_field_value == 2) {
				$this->addElement ( 'Checkbox', $field_key, array ('label' => 'CMSPAGE_FORM_CREATE_CMSPAGE_' . strtoupper ( $field_key ) . '_DESCRIPTION', 'value' => $field_default ) );
				$this->$field_key->clearDecorators ()->addDecorator ( 'ViewHelper' )->addDecorator ( 'Label', array ('placement' => Zend_Form_Decorator_Abstract::APPEND ) )->addDecorator ( 'HtmlTag', array ('tag' => 'div', 'id' => $field_key . '-element', 'class' => 'form-element' ) );
			}
		}
		
		$this->addElement ( 'Checkbox', 'search', array ('label' => 'People can search for this page', 'value' => True ) );
		
		// Buttons
		$this->addElement ( 'Button', 'submit', array ('label' => 'Post Page', 'type' => 'submit', 'ignore' => true, 'decorators' => array ('ViewHelper' ) ) );
		
		$this->addElement ( 'Cancel', 'cancel', array ('label' => 'cancel', 'link' => true, 'prependText' => ' or ', 'decorators' => array ('ViewHelper' ) ) );
		
		$this->addDisplayGroup ( array ('submit', 'cancel' ), 'buttons' );
		$button_group = $this->getDisplayGroup ( 'buttons' );
		$button_group->addDecorator ( 'DivDivDivWrapper' );
	
	}

}

